<template>
  <div class="course-video">
    <el-card>
      <div slot="header">课程相关信息</div>
      <el-form>
        <el-form-item label="视频上传">
          <input type="file" ref="video-file">
        </el-form-item>
        <el-form-item label="封面上传">
          <input type="file" ref="image-file">
        </el-form-item>
        <el-form-item>
          <el-button @click="handleUpload">开始上传</el-button>
          <el-button @click="$router.push({ name: 'course-section', params: { courseId }})">返回</el-button>
        </el-form-item>
        <el-form-item>
          <p v-if="uploadProcess !== 0"> 视频上传:{{ uploadProcess }}%</p>
          <p v-if="transcodeProcess !== 0"> 视频转码:{{ transcodeProcess }}%</p>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
/* eslint-disable */
import { aliyunImageUploadAddressAndAuth, aliyunVideoUploadAddressAndAuth, aliyunTranscode, aliyunTransCodePercent } from '@/services/aliyun-upload'
export default {
  name: 'CourseVideo',
  props: {
    courseId: {
      type: [String, Number],
      required: true
    }
  },
  data () {
    return {
      uploader: null,
      imageUrl: '',
      videoId: '',
      uploadProcess: 0,
      transcodeProcess: 0
    }
  },
  created () {
    this.initUploader()
  },
  methods: {
    handleUpload () {
      const videoFile = this.$refs['video-file'].files[0]
      const imageFile = this.$refs['image-file'].files[0]
      this.uploader.addFile(imageFile)
      this.uploader.addFile(videoFile)
      this.uploader.startUpload()
    },
    initUploader () {
      this.uploader = new window.AliyunUpload.Vod({
        // 阿里账号ID，必须有值
        userId: '1618139964448548',
        // 分片大小默认1 MB，不能小于100 KB
        partSize: 1048576,
        // 并行上传分片个数，默认5
        parallel: 5,
        // 网络原因失败时，重新上传次数，默认为3
        retryCount: 3,
        // 网络原因失败时，重新上传间隔时间，默认为2秒
        retryDuration: 2,
        // 是否上报上传日志到视频点播，默认为true
        enableUploadProgress: true,
        // 开始上传
        onUploadstarted: async (uploadInfo) => {
          console.log(uploadInfo)
          var uploadAddressAndAuth = null
          if (uploadInfo.isImage) {
            const { data } = await aliyunImageUploadAddressAndAuth()
            if (data.code === '000000') {
              uploadAddressAndAuth = data.data
              this.imageUrl = data.data.imageURL
            }
          } else {
            const { data } = await aliyunVideoUploadAddressAndAuth({
              fileName: uploadInfo.file.name,
              imageUrl: this.imageUrl
            })
            if (data.code === '000000') {
              uploadAddressAndAuth = data.data
              this.videoId = data.data.videoId
            }
          }
          this.uploader.setUploadAuthAndAddress(uploadInfo, uploadAddressAndAuth.uploadAuth, uploadAddressAndAuth.uploadAddress,uploadAddressAndAuth.imageId || uploadAddressAndAuth.videoId);
        },
        // 文件上传成功
        onUploadSucceed: function (uploadInfo) {
        },
        // 文件上传失败
        onUploadFailed: function (uploadInfo, code, message) {
        },
        // 文件上传进度，单位：字节
        onUploadProgress: function (uploadInfo, totalSize, loadedPercent) {
          this.uploadProcess = Math.floor(loadedPercent*100)
        },
        // 上传凭证超时
        onUploadTokenExpired: function (uploadInfo) {
        },
        // 全部文件上传结束
        onUploadEnd: async (uploadInfo) => {
          const { data } = await aliyunTranscode({
            lessonId: this.$route.query.lessonId,
            fileId: this.videoId,
            coverImageUrl: this.imageUrl,
            fileName: this.$refs['video-file'].files[0].name
          })
          if (data.code === '000000') {
            const timer = setInterval(async () => {
              const { data } = await aliyunTransCodePercent(this.$route.query.lessonId)
              if (data.code === '000000') {
                this.transcodeProcess = data.data
                if (data.data === 100) {
                  clearInterval(timer)
                }
              }
            }, 1000)
          }
        }
      })
    }
  }
}
</script>

<style>

</style>
